package scottb.cribbage.model {
import mx.collections.ArrayCollection;
import mx.events.CollectionEvent;
import mx.events.PropertyChangeEvent;

[Bindable]
public class Team {
                        
    private var originalName:String;
    public var _name:String;
    public var players:ArrayCollection;
    public var score:int;

    public function Team(n:String,  array:Array) {
                                                     
        originalName = n;
        _name = n;
        players = new ArrayCollection(array);

        players.addEventListener(CollectionEvent.COLLECTION_CHANGE, function(event:CollectionEvent):void {
            updateScore();
        });
    } 
    
    public function set name(value:String):void {
        _name = value;
        if(players.length == 1) {
            players.getItemAt(0).name = value;
        }
    }
    
    public function get name():String {
        return _name;
    }

    public function resetName():void {
        name = originalName;
    }   

    private function updateScore():void {

        var tempScore:int = 0;

        for each(var player:Player in players) {
            tempScore += player.score;
        }

        score = tempScore;
    }
}
}
